package Graph;

import java.util.*;

public class 沉没孤岛 {
    static int[][] dir = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
    static int n = 0;
    static int m = 0;
    static int[][] map;

    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        map = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map[i][j] = in.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            if (map[i][0] == 1) dfs(i, 0);
            if (map[i][m - 1] == 1) dfs(i, m - 1);
        }
        for (int j = 0; j < m; j++) {
            if (map[0][j] == 1) dfs(0, j);
            if (map[n - 1][j] == 1) dfs(n - 1, j);
        }
        for (int i = 0; i < n ; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 1) map[i][j] = 0;
                if (map[i][j] == 2) map[i][j] = 1;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static void dfs(int r, int c) {
        map[r][c] = 2;
        for (int[] d : dir) {
            int nextR = r + d[0];
            int nextC = c + d[1];
            if (nextR < 0 || nextR >= n || nextC < 0 || nextC >= m) continue;
            if (map[nextR][nextC] == 0 || map[nextR][nextC] == 2) continue;
            dfs(nextR, nextC);
        }
    }
}
